from django.shortcuts import get_object_or_404
from rest_framework.permissions import AllowAny, IsAuthenticatedOrReadOnly
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from home_design.models import Inspiration
from home_design.serializers import InspirationSerializer, InspirationListSerializer


# 用户获取自己的灵感作品
class MyInspirationListView(APIView):
    permission_classes = [IsAuthenticatedOrReadOnly]

    def get(self, request):
        """
        处理GET请求，返回所有灵感的列表
        """
        inspirations = Inspiration.objects.filter(designer=request.user)  # 查询所有灵感
        serializer = InspirationSerializer(inspirations, many=True)  # 序列化灵感数据
        return Response(serializer.data, status=status.HTTP_200_OK)  # 返回序列化后的数据


class InspirationListView(APIView):
    """
    处理灵感列表的视图，包括获取所有灵感和创建新灵感
    """
    permission_classes = [IsAuthenticatedOrReadOnly]

    def get(self, request):
        """
        处理GET请求，返回所有灵感的列表
        """
        inspirations = Inspiration.objects.all()  # 查询所有灵感
        serializer = InspirationSerializer(inspirations, many=True)  # 序列化灵感数据
        return Response(serializer.data, status=status.HTTP_200_OK)  # 返回序列化后的数据

    def post(self, request):
        """
        处理POST请求，创建新的灵感
        """
        serializer = InspirationSerializer(data=request.data, context={'request': request})  # 从请求数据中创建序列化器
        if serializer.is_valid():  # 检查数据是否有效
            serializer.save()  # 保存新灵感，设置设计师为当前用户
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class InspirationClassificationView(APIView):
    permission_classes = [IsAuthenticatedOrReadOnly]

    def get(self, request, classification):
        """
        处理GET请求，返回所有灵感的分类
        """
        inspirations = Inspiration.objects.filter(inspiration_classification=classification)
        serializer = InspirationSerializer(inspirations, many=True)  # 序列化灵感数据
        return Response(serializer.data, status=status.HTTP_200_OK)  # 返回序列化后的数据


class InspirationDetailView(APIView):
    """
    处理单个灵感的视图，包括获取、更新和删除灵感
    """
    permission_classes = [IsAuthenticatedOrReadOnly]

    def get_object(self, pk):
        """
        根据主键获取灵感对象，如果不存在则返回404错误
        """
        return get_object_or_404(Inspiration, pk=pk)

    def get(self, request, pk):
        """
        处理GET请求，返回单个灵感的详细信息
        """
        inspiration = self.get_object(pk)  # 获取灵感对象
        serializer = InspirationListSerializer(inspiration,context={'request': request})  # 序列化灵感数据
        return Response(serializer.data, status=status.HTTP_200_OK)  # 返回序列化后的数据

    def put(self, request, pk):
        """
        处理PUT请求，更新单个灵感
        """
        inspiration = self.get_object(pk)  # 获取灵感对象
        serializer = InspirationSerializer(inspiration, data=request.data)  # 从请求数据中更新序列化器
        if serializer.is_valid():  # 检查数据是否有效
            serializer.save()  # 保存更新后的灵感
            return Response(serializer.data, status=status.HTTP_200_OK)  # 返回更新后的数据
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)  # 返回错误信息

    def delete(self, request, pk):
        """
        处理DELETE请求，删除单个灵感
        """
        inspiration = self.get_object(pk)  # 获取灵感对象
        inspiration.delete()  # 删除灵感
        return Response(status=status.HTTP_204_NO_CONTENT)  # 返回删除成功的响应
